package com.ywwl.dao.provider;

import com.ywwl.domain.table.SysRole;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.Map;

public class RoleProvider {

    public String selectByWhere(HashMap<String, String> whereMap) {
        StringBuilder builderWhere = new StringBuilder("SELECT * FROM sys_roles ");
        if (whereMap != null) {
            builderWhere.append("WHERE 1=1 ");
            for (Map.Entry<String, String> entry : whereMap.entrySet()) {
                builderWhere.append("AND " + entry.getKey() + "='" + entry.getValue() + "' ");
            }
        }
        return builderWhere.toString();
    }

    public String list(HashMap<String, String> whereMap, HashMap<String, String> orderMap) {
        StringBuilder builderWhere = new StringBuilder("SELECT * FROM sys_roles ");
        if (whereMap != null) {
            builderWhere.append("WHERE 1=1 ");
            for (Map.Entry<String, String> entry : whereMap.entrySet()) {
                builderWhere.append("AND " + entry.getKey() + "='" + entry.getValue() + "' ");
            }
        }

        if (orderMap != null) {
            builderWhere.append("ORDER BY ");
            for (Map.Entry<String, String> entry : orderMap.entrySet()) {
                builderWhere.append(entry.getKey() + " " + entry.getValue() + ",");
            }
        }

        String strSql = builderWhere.substring(0, builderWhere.length() - 1);

        System.out.println("角色查询SQL:" + strSql);
        return strSql;
    }

    public String insert(SysRole role) {
        return new SQL()
                .INSERT_INTO("sys_roles")
                .VALUES("roleName", "#{roleName}")
                .VALUES("remark", "#{remark}")
                .toString();
    }

    public String update(SysRole role) {
        return new SQL()
                .UPDATE("sys_roles")
                .SET("roleName=#{roleName}")
                .SET("remark=#{remark}")
                .WHERE("id=#{id}")
                .toString();
    }

    public String delete(Integer id){
        return new SQL()
                .DELETE_FROM("sys_roles")
                .WHERE("id=#{id}")
                .toString();
    }
}
